<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加插件</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="//cdn.staticfile.org/layui/2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" lay-filter="plugin-info">
    <div class="layui-form-item">
        <label class="layui-form-label">插件分类:</label>
        <div class="layui-input-block">
            <select lay-verify="required" name="category_id" id="categories"  lay-verify="required" lay-reqtext="请选择分类">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">插件标题:</label>
        <div class="layui-input-block">
            <input type="text" name="title" placeholder="填写插件的标题" lay-verify="required" lay-reqtext="插件标题不能为空"
                   class="layui-input"
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">路由别名:</label>
        <div class="layui-input-block">
            <input type="text" name="alias" placeholder="请输入路由别名" value=""
                   lay-verify="required" lay-reqtext="路由别名不能为空"
                   class="layui-input" required>
            <tip>也就是浏览器地址栏，域名后面的那一串</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">插件类名:</label>
        <div class="layui-input-block">
            <input type="text" name="class" placeholder="请填写插件类名" value=""
                   lay-verify="required" lay-reqtext="插件类名不能为空"
                   class="layui-input" required>
            <tip>插件的类名，包名+插件名，例如：aoaostar_com\example。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">插件关键词:</label>
        <div class="layui-input-block">
            <input type="text" name="keyword" placeholder="插件关键词可留空" value=""
                   class="layui-input">
            <tip>用于搜索的关键词，多个用英文逗号,隔开</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">插件权重:</label>
        <div class="layui-input-block">
            <input type="number" name="weight" placeholder="请填写插件权重" value="0"
                   lay-verify="required" lay-reqtext="插件权重不能为空"
                   class="layui-input" required>
            <tip>填数字，数字越大排名越前。</tip>
        </div>
    </div>
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">是否启用:</label>
        <div class="layui-input-block">
            <input type="checkbox" value="1" name="enable" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF" checked>
            <tip>禁用则不会在前台显示。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否强制登录:</label>
        <div class="layui-input-block">
            <input type="checkbox" value="1" name="login" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户等级限制:</label>
        <div class="layui-input-block">
            <input type="number" name="level" placeholder="填写用户等级数字" value="0"
                   lay-verify="required" lay-reqtext="用户等级限制不能为空"
                   class="layui-input" required>
            <tip>用户等级小于该数字的不会显示该工具</tip>
        </div>
    </div>
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">插件描述:</label>
        <div class="layui-input-block">
            <textarea name="desc" placeholder="插件描述" class="layui-textarea"></textarea>
            <tip>插件描述信息，介绍一下这个插件是干什么的吧！</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script id="categoies_tpl" type="text/html">
    <select lay-verify="required" name="category_id">
        <option value="0">默认组</option>
        {{# layui.each(d, function(index, item){ }}
        <option value="{{ item.id }}">{{ item.title }}</option>
        {{# }); }}
    </select>
</script>
<script id="templates_tpl" type="text/html">
    <select lay-verify="required" name="category_id">
        <option value="default">default</option>
        {{# layui.each(d, function(index, item){ }}
        <option value="{{ item }}">{{ item }}</option>
        {{# }); }}
    </select>
</script>
<script src="//cdn.staticfile.org/layui/2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/api.js?v=1.0.0" charset="utf-8"></script>
<script src="../../js/common.js?v=1.0.0" charset="utf-8"></script>
<script>

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            laytpl = layui.laytpl,
            $ = layui.$;

        //layer.load(1)
        let tasks = []
        tasks.push(httpGet('/admin/category/list').then(res => {
            var getTpl = document.getElementById('categoies_tpl').innerHTML,
                view = document.getElementById('categories');
            laytpl(getTpl).render(res.data.items, function (html) {
                view.innerHTML = html;
                form.render();
            });
        }))
        Promise.all(tasks).finally(() => {
            //layer.closeAll('loading')
        })
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            layer.load(1)
            data.field.enable = data.field.enable ? 1 : 0;
            data.field.login = data.field.login ? 1 : 0;
            httpPost('/admin/plugin/add', data.field).then(res => {
                if (res.status === 'ok') {
                    $message.success('添加成功', {icon: 1});
                    const index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            }).finally(() => {
                layer.closeAll('loading')
            })
            return false;
        });

    });
</script>
</body>
</html>